﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading.Tasks;

namespace MSMQConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string queuepath = @".\private$\myQueue";

            /*User stu = new User() { Name = "shaoshun", Age = 18 };

            QueueManger.SendMessage<User>(stu, queuepath);*/


            /*User stu = QueueManger.ReceiveMessage<User>(queuepath);
            Console.WriteLine(stu.Name);*/


            //MessageQueueTransaction tran = new MessageQueueTransaction();
            //tran.Begin();
           /* try
            {
                for (int i = 0; i < 4; i++)
                {
                    User stu = new User() { Name = "shaoshun" + i, Age = i };
                    QueueManger.SendMessage<User>(stu, queuepath, null);

                    
                    /*if (i == 3)
                    {
                        //测试事务是否正常
                        throw new Exception();
                    }#1#
                }

                //tran.Commit();
                
            }
            catch
            {
                //tran.Abort();
            }
*/
            User stu1 = QueueManger.ReceiveMessage<User>(queuepath);
            Console.WriteLine(stu1.Name);

            Console.ReadKey();

            /*MessageQueue messageQueue = null;
            if (MessageQueue.Exists(@".\Private$\MyQueues"))
            {
                messageQueue = new MessageQueue(@".\Private$\MyQueues");
                messageQueue.Label = "Testing Queue";
            }
            else
            {
                messageQueue = MessageQueue.Create(@".\Private$\MyQueues");
                messageQueue.Label = "Newly Created Queue";
            }
            messageQueue.Send("First ever Message is sent to MSMQ", "Title");

            messageQueue.Formatter = new XmlMessageFormatter(new string[] { "System.String" });
            //iterating the queue contents
            foreach (Message msg in messageQueue)
            {
                string readMessage = msg.Body.ToString();
                Console.WriteLine(readMessage);
                //process message
            }
            //after all processing delete the messages
            messageQueue.Purge();
            Console.ReadKey();*/
        }
    }
}
